package com.xingda.packing.task.generate;

import com.xingda.packing.domain.PackProductionLine;
import com.xingda.packing.domain.ProductBoxLocation;
import com.xingda.packing.enums.AlarmTriggerType;
import com.xingda.packing.enums.TaskState;
import com.xingda.packing.task.ticket.AbstractTaskTicket;
import com.xingda.packing.task.ticket.ScanCodeTicket;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * 扫码
 *
 * @author LEGION
 */
@Order(2)
@Component
@Slf4j
public class ScanCodeGenerate implements TaskGenerateService {

    @Value("${com.xingda.model}")
    private String model;

    @Override
    public AbstractTaskTicket generate(PackProductionLine productionLine, int area) {
        if (!productionLine.getRobot().checkCanAssignTaskTicket(area)) {
            return null;
        }
        if (!productionLine.getRobot().canAssignTask(area)) {
            return null;
        }
        if (productionLine.getRobot().haveWheel(area)) {
            return null;
        }
//        校验未完成的任务
        if (!productionLine.getRobot().checkActionStatus(area)) {
            productionLine.getRobot().createAlarmEvent("当前区域存在未完成任务,已停止机器人,请去后台管理页面:[操作管理-动作操作]查看", area);
            log.info("====ScanCodeGenerate-当前区域存在未完成任务");
            productionLine.setInitRunning(area, false);
            return null;
        }
        if (productionLine.getCacheArea().isFull(area)) {
            productionLine.getRobot().createAlarmEvent("缓存位已到警戒值！已停止系统", area);
            productionLine.getEventCreater().createTriggerAlarm(AlarmTriggerType.CACHE_FULL, true, area);
            productionLine.setInitRunning(area, false);
            return null;
        } else {
            productionLine.getEventCreater().createTriggerAlarm(AlarmTriggerType.CACHE_FULL, false, area);
        }
        //如果有清线装箱 停止系统
        List<ProductBoxLocation> locationList = productionLine.getProductArea().getLocationList(area);
        for (ProductBoxLocation productBoxLocation : locationList) {
            if (productBoxLocation.getBox().getType() == 2) {
                productionLine.getRobot().createAlarmEvent("当前区域存在清线装箱,已停止系统,请重置此装箱", area);
                productionLine.setInitRunning(area, false);
                return null;
            }
        }
        // 如果装箱位都不可作业,不发送去抓取指令
        if (locationList.stream().filter(ProductBoxLocation::isNotEmpty).filter(ProductBoxLocation::isPhotoFinish).noneMatch(ProductBoxLocation::canWork)) {
            log.info("区域{}->无可作业装箱位", area);
            return null;
        }
        ScanCodeTicket ticket = new ScanCodeTicket();
        ticket.setState(TaskState.NEW);
        if (!productionLine.getRobot().is100HaveWheel(area) && !productionLine.getRobot().is101HaveWheel(area)) {
            return null;
        }
        if (productionLine.getRobot().isScanSignal(area)) {
            log.info("区域{}->-----------触发为true", area);
            return null;
        }

//      抓取点的选取

//        测试用
        log.info("==== ScanCodeGenerate的model:{}", model);

        if (model.equals("DMDS")) {
//            双抓取点
            if (productionLine.getRobot().getTagValue(area)) {

                //        测试用
                log.info("==== ScanCodeGenerate的抓取点判断1:{}", productionLine.getRobot().getTagValue(area));

                ticket.setLocationNo(productionLine.getRobot().getLastGrabSum(area));
            } else if (productionLine.getRobot().is100HaveWheel(area) && !productionLine.getRobot().is101HaveWheel(area)) {

//                测试用
                log.info("==== ScanCodeGenerate的抓取点判断2:{}", productionLine.getRobot().is100HaveWheel(area) && !productionLine.getRobot().is101HaveWheel(area));

                ticket.setLocationNo(100);
            } else if (!productionLine.getRobot().is100HaveWheel(area) && productionLine.getRobot().is101HaveWheel(area)) {

//                测试用
                log.info("==== ScanCodeGenerate的抓取点判断3:{}", !productionLine.getRobot().is100HaveWheel(area) && productionLine.getRobot().is101HaveWheel(area));

                ticket.setLocationNo(101);
            } else if (productionLine.getRobot().is100HaveWheel(area) && productionLine.getRobot().is101HaveWheel(area)) {

//                测试用
                log.info("==== ScanCodeGenerate的抓取点判断4:{}", productionLine.getRobot().is100HaveWheel(area) && productionLine.getRobot().is101HaveWheel(area));

                ticket.setLocationNo(productionLine.getRobot().chooseGrabNum(area));
            }
        } else {
//            单抓取点
            ticket.setLocationNo(100);
        }


        productionLine.getRobot().goZeroGrab(ticket.getLocationNo(), area);
        log.info("区域{}->发送去往抓取位指令,抓取位置{}", area, ticket.getLocationNo());
        productionLine.getRobot().createScanCodeEvent(ticket, area);
        log.info("区域{}->创建扫码任务，抓取位：{}", area, ticket.getLocationNo());
        return ticket;
    }

}
